/**************************
void * memset ( void * ptr, int value, size_t num );
Fill block of memory

Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).

Parameters
ptr		Pointer to the block of memory to fill.
value	Value to be set. The value is passed as an int, but the function fills the block of memory using the unsigned char conversion of this value.
num		Number of bytes to be set to the value.

Return Value
ptr is returned.

******************************

int memcmp ( const void * ptr1, const void * ptr2, size_t num );
Compare two blocks of memory

Compares the first num bytes of the block of memory pointed by ptr1 to the first num bytes pointed by ptr2, 
returning zero if they all match or a value different from zero representing which is greater if they do not.

Parameters
ptr1	Pointer to block of memory.
ptr2	Pointer to block of memory.
num		Number of bytes to compare.

Return Value
Returns an integral value indicating the relationship between the content of the memory blocks:
A zero value indicates that the contents of both memory blocks are equal.
A value greater than zero indicates that the first byte that does not match in both memory blocks has a greater value 
in ptr1 than in ptr2 as if evaluated as unsigned char values; And a value less than zero indicates the opposite.

*******************************

void * memcpy ( void * destination, const void * source, size_t num );
Copy block of memory

Copies the values of num bytes from the location pointed by source directly to the memory block pointed by destination.

The underlying type of the objects pointed by both the source and destination pointers are irrelevant for this function; 
The result is a binary copy of the data.

The function does not check for any terminating null character in source - it always copies exactly num bytes.

To avoid overflows, the size of the arrays pointed by both the destination and source parameters, shall be at least num bytes,
and should not overlap (for overlapping memory blocks, memmove is a safer approach).

Parameters
destination		Pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*.
source			Pointer to the source of data to be copied, type-casted to a pointer of type void*.
num				Number of bytes to copy.

Return Value
destination is returned.

 *******************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MAX_SIZE 5

char data1[MAX_SIZE];
char data2[MAX_SIZE];

int init_data(char *d,char val,int size)
{
	memset(d,val,size);
	return 0;
}

int show_data(char *d,int size)
{
	int i;
	for(i=0;i<size;i++)
	{
		printf("%d\t",*d++);
	}
	printf("\n");
}

int copy_data(char *dst,char *src,int size)
{
	memcpy(dst,src,size);

	return 0;
}

int compare_data(char *d1,char *d2,int size)
{
	int ret;
	ret = memcmp(d1,d2,size);
	
	return ret;
}

void test_case1()
{
	int result;

	init_data(data1,1,MAX_SIZE);
	init_data(data2,2,MAX_SIZE);
	
	printf("data1:\n");
	show_data(data1,MAX_SIZE);
	printf("data2:\n");
	show_data(data2,MAX_SIZE);
	
	result = compare_data(data1,data2,MAX_SIZE);
	
	if(result == 0)
	{
		printf("data1 is equal to data2\n");
	}
	else if(result > 0)
	{
		printf("data1 is bigger than data2\n");
	}
	else
	{
		printf("data1 is smaller than data2\n");
	}
}

void test_case2()
{
	int result;

	init_data(data1,5,MAX_SIZE);
	init_data(data2,5,MAX_SIZE);
	
	printf("data1:\n");
	show_data(data1,MAX_SIZE);
	printf("data2:\n");
	show_data(data2,MAX_SIZE);
	
	result = compare_data(data1,data2,MAX_SIZE);
	
	if(result == 0)
	{
		printf("data1 is equal to data2\n");
	}
	else if(result > 0)
	{
		printf("data1 is bigger than data2\n");
	}
	else
	{
		printf("data1 is smaller than data2\n");
	}
}

void test_case3()
{
	int result;

	init_data(data1,8,MAX_SIZE);
	init_data(data2,9,MAX_SIZE);
	
	printf("data1:\n");
	show_data(data1,MAX_SIZE);
	printf("data2:\n");
	show_data(data2,MAX_SIZE);

	result = compare_data(data1,data2,MAX_SIZE);
	
	if(result == 0)
	{
		printf("data1 is equal to data2\n");
	}
	else if(result > 0)
	{
		printf("data1 is bigger than data2\n");
	}
	else
	{
		printf("data1 is smaller than data2\n");
	}

	printf("copy data1 to data2\n");
	copy_data(data2,data1,MAX_SIZE);

	printf("data1:\n");
	show_data(data1,MAX_SIZE);
	printf("data2:\n");
	show_data(data2,MAX_SIZE);

	result = compare_data(data1,data2,MAX_SIZE);
	
	if(result == 0)
	{
		printf("data1 is equal to data2\n");
	}
	else if(result > 0)
	{
		printf("data1 is bigger than data2\n");
	}
	else
	{
		printf("data1 is smaller than data2\n");
	}

}

int main()
{
	test_case1();
	test_case2();
	test_case3();

	return 0;
}
